$(function () {
	//发送私信框
   $('.l-reply,.send').click(function () {
      var username = '';
      var letterLeft = ($(window).width() - $('#letter').width()) / 2;
      var letterTop = $(document).scrollTop() + ($(window).height() - $('#letter').height()) / 2;
      var obj = $('#letter').show().css({
        'left' : letterLeft,
        'top' : letterTop
      });

       if ($(this).attr('class') == 'l-reply') {
           username = $(this).attr('uname');
           obj.find("#cp-uname").val(username);
           obj.find("#cp-uname").attr('disabled','disabled');
           obj.find('textarea').focus();
       } else {
           obj.find("#cp-uname").val('');
           obj.find("#cp-uname").removeAttr('disabled');
           obj.find("#cp-uname").focus();
       }

      createBg('letter-bg');
      drag(obj, obj.find('.letter_head'));
   });
   //关闭
   $('.letter-cencle').click(function () {
   		$('#letter').hide();
   		$('#letter-bg').remove();
   });

    /**
     * 发送私信
     */
    $("#letter-data").on('submit',function(){
        var uname = $("#cp-uname").val();
        var content = $("#cp-content").val();
        var sendUrl = $(this).attr('url');
        if (!uname || !content) {
            layer.msg('昵称或者内容不能为空.^_^');
            return false;
        }
        var load = layer.load(1, {time: 5*1000});
        $.post(sendUrl,{'username' : uname,'content' : content},function(data){
            layer.close(load);
            if (data.status) {
                layer.msg(data.msg,{'icon' : 6,'time' : 2000},function(){
                    $('.letter-cencle').click();
                });
            } else {
                layer.msg(data.msg);
            }
        },"json");
        return false;
    });

   /**
    * 删除私信
    */
   $('.del-letter').on('click',function () {
      var lid = $(this).attr('lid');
      var obj = $(this).parents('dl');
      var url = $(this).attr('url');
      layer.confirm('确认删除该私信？', {icon: 3, title:'提示'}, function(index){
           //点击确认才能回调
           layer.close(index);
          $.post(url, {lid : lid}, function (data) {
              if (data.status) {
                  obj.slideUp('slow', function () {
                      obj.remove();
                  });
              } else {
                  layer.msg(data.msg);
              }
          }, 'json');
      });
   })
})